package com.cms.common.util.pass;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

import org.apache.commons.codec.binary.Base64;

import com.cms.common.kit.StrKit;

/**
 * 通过钥匙进行加密。
 * @author Administrator
 *
 */
public class SystemDecoder {
	//simcms0.1
	private static final byte[] E_KEY ={'s','i','m','c','m','s','0','1'};
	
	/**
	 * 加密
	 * @param value
	 * @return 
	 * @throws Exception 
	 */
	public static String _Encrypt(String value) {
		byte[] data = CryptUtils.encrypt(value.getBytes(), E_KEY);
		try{
			return URLEncoder.encode(new String(Base64.encodeBase64(data)),"utf8");
		}catch(UnsupportedEncodingException e){
			return null;
		}
	}

	/**
	 * 解密
	 * @param value
	 * @return
	 * @throws Exception 
	 */
	public static String _Decrypt(String value) {
		try {
			value = URLDecoder.decode(value,"utf8");
			if(StrKit.isBlank(value)) return null;
			byte[] data = Base64.decodeBase64(value.getBytes());
			return new String(CryptUtils.decrypt(data, E_KEY));
		} catch (UnsupportedEncodingException excp) {
			return null;
		}
	}
	
	public static void main(String[] args) {
		System.out.println(_Encrypt("xiaoxiao@123"));
		System.out.println(_Decrypt(_Encrypt("xiaoxiao@123")));
	}
}
